
var counter =10;

function LoadEmailBody()
{
var emailBody = document.getElementById("hidEmailBodyContent");
EnableSlugSupport();

if(!IsNull(emailBody))
{
try
{
var disabled = crmForm.all.description.Disabled;
crmForm.all.description.Disabled = false;
crmForm.all.description.LoadMessageContent(CrmEncodeDecode.CrmXmlDecode(emailBody.value));
crmForm.all.description.Disabled = disabled;

}
catch(e)
{

counter--;
if(counter>0)
{
window.setTimeout(LoadEmailBody,200);
}
}

}


window.setTimeout(SetFocusForEmailForm,50);

}

function SetFocusForEmailForm()
{
try
{
SetFieldFocusAtOnLoad(_firstElement);
}
catch(e)
{}
}

function EnableSlugSupport()
{
crmForm.all.description.SupportSlug = true;
}

function ValidateSendEmailTemplate(noValidation)
{

var selectedRow = crmGrid.InnerGrid.SelectedRecords;

if(selectedRow.length==0)
{
if( IsNull(noValidation) || !noValidation )
{
alert(LOCID_NO_TEMPLATE_SELECTED);
}

return null;
}


return selectedRow[0][0];
}

function SelectGrid()
{
if(!SetGridSelection())
{
document.all.templateTypeList.DataValue="1";
RefreshTemplateGrid();


window.setTimeout(SetGridSelection,10);
}
}

function SetGridSelection()
{
var gridRow =  crmGrid.InnerGrid;
var templateId = hidTemplateId.value
if( IsNull(templateId) || templateId=="" )
{
return true;
}
templateId =templateId.toUpperCase();
for(var i=0;i<gridRow.rows.length;i++)
{
if(gridRow.rows[i].oid == templateId)
{
crmGrid.InnerGrid.UnselectRecords();
gridRow.SelectRecords(i,i);
return true;
}
}
return false;
}

function HideNewTemplateButton()
{
try
{
document.all._MBCreateNewTemplate.disabled = true;
document.all.templateTypeList.attachEvent("onchange",DropdownChange);
document.all._MBdoActioncrmGrid2010delete.disabled = true;
}
catch(e)
{
document.all.templateTypeList.disabled = true;

}
}
function DropdownChange()
{
RefreshTemplateGrid();
}

function RefreshTemplateGrid()
{
crmGrid.SetParameter("CurrentSelect",document.all.templateTypeList.DataValue);
crmGrid.Reset();
}

function InsertKBArticle()
{
var sUrl = prependOrgName("/CS/dialogs/KBSearch.aspx?HideEmailButton=true");
var oLookupItems = openStdDlg(sUrl, null, 700, 500);

if (!IsNull(oLookupItems))
{
var oId = oLookupItems.items[0].id;
if (!IsNull(oId))
{
sUrl = prependOrgName("/CS/articles/viewer/content.aspx?id="+oId);
var oXmlHttp = CreateXmlHttp();
oXmlHttp.open("POST", sUrl, false);
oXmlHttp.send("");
if (oXmlHttp.responseText != null)
{
crmForm.all.description.InsertValue(oXmlHttp.responseText);
}
}
}
}
